package com.zhongkai.app.interceptor;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.zhongkai.app.service.PowerSyetemService;
import com.zhongkai.model.utils.ZhongkaiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    private static final String[] allowUrls = {"/app/","/app_uploa/img2","/app/get_phone","/app/get_userxy","/weixiu/puk"};  //不需要登录可以访问的接口
    private static final String[] roleUrls = {"/tcp", "/device/switch"};  //不需要权限的接口
    @Autowired
    private PowerSyetemService powerSyetemService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {


        String stringUrl = httpServletRequest.getRequestURL().toString();
       // System.out.println(stringUrl);
        for (String allowUrl : allowUrls) {

            if (stringUrl.contains(allowUrl)) { //访问非限制接口 放行
                // System.out.println("放行");
                return true;
            }
        }

        //-----------------------------------需要登录才能访问的接口


        //String token = CookieUtils.getCookieValue(httpServletRequest, "token");
        //System.out.println(token);
        String  token = httpServletRequest.getParameter("token");
/*        if (StringUtils.isEmpty(token)) {

        }*/

        //  token = "啊";

        if (!StringUtils.isEmpty(token)) { //访问非限制接口 token中有用户信息

            ZhongkaiResult userToken = powerSyetemService.getUserToken2(token); //取出用户数据

            if (userToken.getStatus() != 200) {  //登录状态过期

                this.returnJson(httpServletResponse, new Gson().toJson(userToken));

                return false;
            } else {
                //取出用户信息
/*                Chinaset chinaset = (Chinaset) userToken.getData();
                httpServletRequest.setAttribute("user", chinaset);*/
                return true; //放行


            }
        } else {

            this.returnJson(httpServletResponse, new Gson().toJson(ZhongkaiResult.build(3001, "请先登录")));

            return false;
        }


    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }


    private void returnJson(HttpServletResponse response, String json) throws Exception {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(json);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
                writer.close();
        }
    }

}
